package com.graduation_design.modules.system.controller;

import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.SysMenu;
import com.graduation_design.service.SysMenuService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
    // 菜单接口开发
    // @RequiresPermissions表示有该权限标识的用户，才能访问到请求

    @Resource
    SysMenuService sysMenuService;

    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public Result list() {
        return Result.success(sysMenuService.tree());
    }

    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:menu:list")
    public Result info(@PathVariable("id") Long id) {
        return Result.success(sysMenuService.getById(id));
    }

    /**
     * 需要sys:menu:save或sys:menu:update操作权限才能访问当前接口
     */
    @PostMapping("/save")
    @RequiresPermissions(value = {"sys:menu:save", "sys:menu:update"}, logical = Logical.OR)
    public Result save(@Validated @RequestBody SysMenu sysMenu) {
        if (sysMenu.getId() == null) {
            sysMenu.setCreated(LocalDateTime.now());
        } else {
            sysMenu.setUpdated(LocalDateTime.now());
        }
        sysMenuService.saveOrUpdate(sysMenu);
        return Result.success(sysMenu);
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys:menu:delete")
    public Result delete(Long id) {
        sysMenuService.delete(id);
        return Result.success();
    }
}
